﻿using MangoMes.Commont.DapperHelps;
using MangoMes.Commont.Domain.ProductionWorking;
using MangoMes.Commont.Domain.SellStocks;
using MangoMes.Commont.DTOs;
using MangoMes.Commont.Infrastructures.Interfaces;
using MediatR;

namespace MangoMes.Write.API.Applications.ProductionWrite
{
    public class ProductionUpdateHandler : IRequestHandler<ProductionUpdateCommand, ResultsDTO>
    {
        private readonly IBateRepository<ProductionWork> _productionRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="productionRepository"></param>
        public ProductionUpdateHandler(IBateRepository<ProductionWork> productionRepository)
        {
            _productionRepository = productionRepository;
        }
        /// <summary>
        /// 生产报工修改
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ResultsDTO> Handle(ProductionUpdateCommand request, CancellationToken cancellationToken)
        {
            var list = new ResultsDTO() { Code = 0 };

            //连接数据库
            DapperHelper dapper = new DapperHelper();
            var conn = dapper.MangoMesWrite;

            //实体
            var sellStock = new ProductionWork();
            var str = "update productionwork set ProductionWorkNo='" + request.ProductionWorkNo + "',ProductionTypeId='" + request.ProductionTypeId + "',ProductionWorkOrdersID='" + request.ProductionWorkOrdersID + "',PickingNode='" + request.PickingNode + "',ProductionWorkNum='" + request.ProductionWorkNum + "'," +
                "departmentId='" + request.departmentId + "',ProductionWorkTime='" + request.ProductionWorkTime + "',ProductionStart='" + request.ProductionStart + "'" +
                ",ProductionWorkRemark='" + request.ProductionWorkRemark + "',ProductionWorkState='" + request.ProductionWorkState + "' where ProductionWorkId='" + request.ProductionWorkId + "'";
            var result = _productionRepository.Add(str, sellStock, conn);
            if (result != null)
            {
                list.Code = 1;
                list.Message = "修改成功";
            }
            return list;
        }
    }
}
